package com.lanou.dllo.findfood.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.MenuResult;
import com.lanou.dllo.findfood.util.MenuClickLisetener;
import com.lanou.dllo.findfood.util.Menuimage;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class MenuReclyclerAdpter extends RecyclerView.Adapter<MenuReclyclerAdpter.ReclyclerAdpter>  {
    private List<MenuResult> resultList;
    private RequestQueue queue;
    private MenuClickLisetener lisetener;
    private String dea;
    private Context context;
    private VolleySington volleySington;
    private View view;

    private Menuimage mlisetener;

    public void setOnclickLisetener(MenuClickLisetener menuClickLisetener) {
        lisetener = menuClickLisetener;
    }

    public  void  setImageOnclick(Menuimage listener){
        mlisetener = listener;

    }

    public void addData(List<MenuResult> results) {
        resultList = results;
        notifyDataSetChanged();


    }

    public MenuReclyclerAdpter(Context con) {
        queue = Volley.newRequestQueue(con);
        this.context = con;
        volleySington = VolleySington.getInstance();
    }
    public  MenuReclyclerAdpter(){

    }

    //加载行布局文件
    @Override
    public ReclyclerAdpter onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        view = inflater.inflate(R.layout.rececy_beauty_item, null);

        return new ReclyclerAdpter(view);
    }


    //该类中添加内容
    @Override
    public void onBindViewHolder(ReclyclerAdpter holder, int position) {
        if (resultList != null && resultList.size() > 0) {
            MenuResult result = resultList.get(position);


            holder.name.setText(result.getName());
            // String str=re.getKeywords();
            String[] strarray = result.getKeywords().split("[ ]");
            for (int i = 0; i < strarray.length; i++)
                System.out.println(strarray[i]);
            {
                String k = strarray[0];
                String k2 = strarray[1];
                String k3 = strarray[2];
                String k4 = strarray[3];
                String k5 = strarray[4];

                holder.key.setText(k);
                holder.key1.setText(k2);
                holder.key2.setText(k3);
                holder.key3.setText(k4);
                holder.key4.setText(k5);
                // holder.needfood.setText(result.getFood());
            }

                if (result.getImg() != null && result.getImg().length() > 0) {
                    String imgurl = result.getImg();
                    String url = " http://tnfs.tngou.net/image/" + imgurl;
                    getBitmap(url, holder.images);
                }

            }

            holder.postion = position;

        }



    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = volleySington.getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }


    @Override
    public int getItemCount() {
        return resultList != null && resultList.size() > 0 ? resultList.size() : 0;
    }


    //内部类  初始化行布局中的组件
    class ReclyclerAdpter extends RecyclerView.ViewHolder implements View.OnClickListener{
        private NetworkImageView images;
        private TextView name, menudeals;
        private TextView key,key1,key2,key3,key4;
        private TextView details;
        private int postion;
        private int id;

        public ReclyclerAdpter(View itemView) {
            super(itemView);
            images = (NetworkImageView) itemView.findViewById(R.id.titleC);
            name = (TextView) itemView.findViewById(R.id.titlecity);
            key = (TextView) itemView.findViewById(R.id.key);
            key1= (TextView) itemView.findViewById(R.id.key1);
            key2= (TextView) itemView.findViewById(R.id.key2);
            key3= (TextView) itemView.findViewById(R.id.key3);
            key4= (TextView) itemView.findViewById(R.id.key4);
            details = (TextView) itemView.findViewById(R.id.click);
            menudeals = (TextView) itemView.findViewById(R.id.discreption);
            details.setOnClickListener(this);
            images.setOnClickListener(this);

        }
    private int count = 0;//0代表设置内容
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.click:
                    if (count==0) {
                        if (resultList != null && resultList.size() > 0){
                            MenuResult result = resultList.get(postion);
                            String str = result.getDescription();
                            menudeals.setText(str);
                        }
                        count = 1;
                    }else if (count==1) {
                        menudeals.setText(null);
                        count = 0;
                    }else {
                        menudeals.setText(null);
                    }

              //

                    break;
                case R.id.titleC:
                    MenuResult result = resultList.get(postion);
                    if (mlisetener != null){
                        mlisetener.MenuimageOnclick(result);
                    }

                break;

                default:
                    break;
            }
        }


    }


}
